<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Report
 *
 * @author Regan
 */
class Report extends AppModel {
    public $useTable = false;
    public $useDbConfig = 'logging';
    
    public function getImportReportData($startdate, $enddate)
    {
        $sql = "select a.ImportDatetime, a.TotalCARRecord, a.id, a.ArchiveFileName"
                . ", b.Message, b.CAR "
                . "from importlogging a "
                . "inner join importloggingdetail b "
                . "on a.id = b.importloggingid "
                . "where a.ImportDatetime >= '$startdate' and a.ImportDatetime < '$enddate'";
        $sqlResult = $this->query($sql);
        
        $reportData = '';
        $headerid = '';
        $prevheaderid = '';
        $counter = -1;
        
        foreach($sqlResult as $obj)
        {
            $headerid = $obj['a']['id'];
            if($prevheaderid != $headerid)
            {
                $counter++;
                $reportData[$counter]['TglImport'] = $obj['a']['ImportDatetime'];
                $reportData[$counter]['ProcessedRecords'] = $obj['a']['TotalCARRecord'];
                
                $filename = explode('_', $obj['a']['id']);
                $reportData[$counter]['FileName'] = $filename[0];
                $reportData[$counter]['ArchiveFileName'] = $obj['a']['ArchiveFileName'];
                
                $reportData[$counter]['Description'] = $obj['b']['CAR'] . ' ' . $obj['b']['Message'];
            }
            else
            {
                $reportData[$counter]['Description'] = $reportData[$counter]['Description'] . '<br />' . $obj['b']['CAR'] . ' ' . $obj['b']['Message'];
            }
            
            $prevheaderid = $headerid;
        }
        
        $countercheck = 0;
        if($reportData != '')
        {
            foreach ($reportData as $objcheck)
            {
                if(strpos($objcheck['Description'], 'Failed') === false)
                {
                    $reportData[$countercheck]['Status'] = 'Success';
                }
                else
                {
                    $reportData[$countercheck]['Status'] = 'Failed';
                }
                $countercheck++;
            }
        }
        
        return $reportData;
    }
    
    public function getExportReportData($startdate, $enddate)
    {
        $sql = "select a.datetime, a.car, coalesce(a.description, '') as Status "
                . "from tbllog a "
                . "where a.datetime >= '$startdate' and a.datetime < '$enddate'";
        $sqlResult = $this->query($sql);
        
        $counter = 0;
        $reportData = '';
        
        foreach($sqlResult as $obj)
        {   
            $reportData[$counter]['TglGenerate'] = $obj['a']['datetime'];
            $reportData[$counter]['AJU'] = $obj['a']['car'];

            $reportData[$counter]['Status'] = $obj[0]['Status'];
            
            $counter++;
        }
        
        return $reportData;
    }
}
